package com.zrc.module_find.activity.defrecycleview.demo1

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import kotlin.jvm.JvmOverloads
import android.widget.RelativeLayout
import android.widget.LinearLayout
import android.view.ViewGroup
import android.view.View
import androidx.core.view.children
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList
import kotlin.math.abs

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
class HRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : RelativeLayout(context, attrs, defStyleAttr) {
    private lateinit var ll_move: LinearLayout
    private lateinit var mRecyclerView: RecyclerView//展示数据时使用的RecycleView
    private var mAdapter: Any? = null //RecycleView的Adapter
    private var mMoveViewList: ArrayList<View> = arrayListOf() //需要滑动的View集合
    private var mStartX = 0f //手指按下时的位置
    private var mMoveOffsetX = 0//滑动时和按下时的差值
    private var mFixX = 0 //最大可滑动差值

    private fun initView() {
        val linearLayout = LinearLayout(context)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.addView(createMoveRecyclerView())
        addView(linearLayout, LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
    }

    //创建数据展示布局
    private fun createMoveRecyclerView(): View {
        val linearLayout = RelativeLayout(context)
        mRecyclerView = RecyclerView(context)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        mRecyclerView.layoutManager = layoutManager
        if (null != mAdapter) {
            if (mAdapter is CommonAdapter<*>) {
                mRecyclerView.adapter = mAdapter as CommonAdapter<*>
                mMoveViewList = (mAdapter as CommonAdapter<*>).moveViewList
            }
        }
        linearLayout.addView(mRecyclerView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
        return linearLayout
    }

    //设置adapter
    fun setAdapter(adapter: Any, ll_move: LinearLayout) {
        this.ll_move=ll_move
        mAdapter = adapter
        initView()
    }

    private fun rightTitleTotalWidth(): Int {
        var d=0
        this.ll_move.children.forEach {
            d+=it.width
        }
        return d
    }

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.e("ZRC","onInterceptTouchEvent***************ACTION_DOWN")
                mStartX=event.x
            }
            MotionEvent.ACTION_MOVE -> {
                Log.e("ZRC","onInterceptTouchEvent***************ACTION_MOVE")
                return abs(event.x - mStartX)>0
            }

            MotionEvent.ACTION_UP -> {
                Log.e("ZRC","onInterceptTouchEvent***************ACTION_UP")
                return false
            }
        }
        return super.onInterceptTouchEvent(event)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mStartX=event.x
                return super.onTouchEvent(event)
            }
            MotionEvent.ACTION_MOVE -> {
                mMoveOffsetX = (mStartX - event.x + mFixX).toInt()
                if (abs(mMoveOffsetX)>0) {
                    if (0 > mMoveOffsetX) {
                        mMoveOffsetX = 0
                    } else {
                        //当滑动大于最大宽度时，不在滑动（右边到头了）
                        if (this.ll_move.width + mMoveOffsetX > rightTitleTotalWidth()) {
                            mMoveOffsetX = rightTitleTotalWidth() - this.ll_move.width
                        }
                    }
                    //跟随手指向右滚动
                    this.ll_move.scrollTo(mMoveOffsetX, 0)
                    mMoveViewList.forEach {
                        it.scrollTo(mMoveOffsetX, 0) //使每个item随着手指向右滚动
                    }
                    return true
                }
                return super.onTouchEvent(event)
            }
            MotionEvent.ACTION_UP -> {
                mFixX = mMoveOffsetX //设置最大水平平移的宽度
                (mAdapter as CommonAdapter<*>).setFixX(mFixX)//每次左右滑动都要更新CommonAdapter中的mFixX的值
                return super.onTouchEvent(event)
            }

            else->return super.onTouchEvent(event)
        }
    }
}